package com.ai.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.service.CommentUseService;
import com.ai.web.vo.CommentUseV;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.ResultModel;

/**
 * 设备使用评价
* @ClassName:  
* @Description: TODO 
* @author:  
* @date: 2014年3月24日
*
 */
@Controller
@RequestMapping("/commentUse")
public class CommentUseController extends BaseController {
    

    private static Logger logger = Logger.getLogger(CommentUseController.class);
    
    @Autowired
    private CommentUseService commentUseService;
    
    @RequestMapping(value = "/list")
    public String list() {
        
        logger.info("hello......test");
        return "commentUse/commentUse_list";
    }
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        QueryData qd = commentUseService.queryListAndCount(conditionMap);
        
        return qd;
    }
    
    @RequestMapping(value = "/new")
    public String newcommentUse(Model model) {
        
        CommentUseV commentUseV = new CommentUseV();
        model.addAttribute(commentUseV);
        
        return "commentUse/commentUse_template";
    }
    
    
    @RequestMapping(value = "/update/{id}")
    public String updatecommentUse(@PathVariable("id") int id, Model model)
            throws Exception {
        
        CommentUseV commentUseV = commentUseService.getCommentUse(id);
        logger.info("----- getUser:" + commentUseV);
        
        if (commentUseV == null) {
            // exception redirect error page
            // TODO
        }
        model.addAttribute(commentUseV);
        
        return "commentUse/commentUse_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel savecommentUse(CommentUseV commentUseV) throws Exception {
        
        logger.info("-----save:" + commentUseV);
        
        
        ResultModel rm = commentUseService.saveCommentUse(commentUseV);
        
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deletecommentUse(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> userIds = getDeleteIds(delIds);
        ResultModel rm = commentUseService.deleteCommentUses(userIds);
        
        return rm;
    }
    
    
}
